package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DepartmentMapper;
import com.kkb.mapper.DoctorMapper;
import com.kkb.pojo.Department;
import com.kkb.pojo.DepartmentExample;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.vo.DoctorQueryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DoctorService {
    @Resource
    DoctorMapper doctorMapper;

    public PageInfo<Doctor> queryAll(int index, int size, DoctorQueryVO doctorQueryVO) {
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        System.out.println(doctorQueryVO.toString());
        if (doctorQueryVO.getName() != null && !doctorQueryVO.getName().trim().equals("")) {
            criteria.andNameLike("%" + doctorQueryVO.getName().trim() + "%");
        }
        if (doctorQueryVO.getId() != null && !doctorQueryVO.getId().trim().equals("")) {
            criteria.andIdEqualTo(Integer.parseInt(doctorQueryVO.getId().trim()));
        }
        if (doctorQueryVO.getDepartment() != null && !doctorQueryVO.getDepartment().trim().equals("")) {
            criteria.andDepartmentIdEqualTo(Integer.parseInt(doctorQueryVO.getDepartment().trim()));
        }

        PageHelper.startPage(index, size);
        List<Doctor> result = doctorMapper.selectByExample(doctorExample);
        PageInfo pageInfo = new PageInfo(result);
        return pageInfo;
    }

    public Doctor queryById(int id) {
        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        return doctor;
    }

    public int update(Doctor doctor) {
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }

    public int add(Doctor doctor){
        return doctorMapper.insert(doctor);
    }

    public List<Doctor> selectByDepartmentId(Integer id){
        if (id==null){
            return null;
        }
        DoctorExample doctorExample = new DoctorExample();
        doctorExample.createCriteria().andDepartmentIdEqualTo(id);
        return doctorMapper.selectByExample(doctorExample);
    }
}
